package com.work.game.home.ui.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.android.volley.Request;
import com.work.game.R;
import com.work.game.ad.MediationTablePlaqueAd;
import com.work.game.ad.kaku.interstitia.TaKuInterstitialAd;
import com.work.game.ad.kaku.interstitia.TaKuInterstitialAdWithdrawalRecord;
import com.work.game.config.UrlConfig;
import com.work.game.home.ui.adapter.WithdrawalRecordAdapter;
import com.work.game.home.ui.enums.RedisEnum;
import com.work.game.home.ui.model.WithdrawalRecord;
import com.work.game.refresh.LoadingFooter;
import com.work.game.refresh.xRecyclerView;
import com.work.game.volley.BaseActivity;
import com.work.game.volley.state.HUCState;

import java.util.ArrayList;
import java.util.List;

public class WithdrawalRecordActivity extends BaseActivity {
    private xRecyclerView xRecyclerView;
    private WithdrawalRecordAdapter withdrawalRecordAdapter;
    private boolean refreshFlag = true;
    private List<WithdrawalRecord> withdrawalRecordList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_withdrawal_record);
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String tag = intent.getStringExtra("tag");
        switch (Integer.valueOf(tag)) {
            case HUCState.HUC_REQEUST_CODE_1:
                tvActionbarTitle.setText("金币记录");
                break;
            case HUCState.HUC_REQEUST_CODE_2:
                tvActionbarTitle.setText("红包记录");
                break;
            case HUCState.HUC_REQEUST_CODE_3:
                tvActionbarTitle.setText("红包佣金记录");
                break;
            case HUCState.HUC_REQEUST_CODE_4:
                tvActionbarTitle.setText("金币佣金记录");
                break;
            case HUCState.HUC_REQEUST_CODE_5:
                tvActionbarTitle.setText("推广佣金记录");
                break;
        }
        pageVo.setQueryValue(tag);
        withdrawalRecordAdapter = new WithdrawalRecordAdapter(this, withdrawalRecordList, R.layout.item_withdrawal_record);
        xRecyclerView.setAdapter(this, withdrawalRecordAdapter, new LinearLayoutManager(this));
        xRecyclerView.setOnRefreshListener(this);
        String taKu = getSharedPreferences(RedisEnum.TA_KU.getKey());
        if (RedisEnum.TA_KU.getValue().equals(taKu)) {
            TaKuInterstitialAdWithdrawalRecord taKuInterstitialAdWithdrawalRecord = TaKuInterstitialAdWithdrawalRecord.getInstance();
            taKuInterstitialAdWithdrawalRecord.setToken(getSharedPreferences("deviceId"));
            taKuInterstitialAdWithdrawalRecord.showAd(this);
        } else {
            MediationTablePlaqueAd mediationTablePlaqueAd = MediationTablePlaqueAd.getInstance();
            mediationTablePlaqueAd.showCacheInterstitialFullAd(this, null);
        }
    }

    @Override
    public void initView() {
        super.initView();
        xRecyclerView = findViewById(R.id.xrecyclerView);
    }

    /**
     * 下拉刷新
     *
     * @param refreshLayout
     */
    @Override
    public void refresh(SwipeRefreshLayout refreshLayout) {
        refreshFlag = false;
        onStart();
    }

    /**
     * 上拉加载
     *
     * @param recyclerView
     */
    @Override
    public void loadMore(RecyclerView recyclerView) {
        refreshFlag = true;
        int CurrentCount = Integer.parseInt(pageVo.getCurrent()) + 1;
        pageVo.setCurrent(CurrentCount + "");
        pageVo.setSize(10 + "");
        getInitData();
    }

    private void getInitData() {
        pageVo.setToken(getSharedPreferences("deviceId"));
        hucRequestInterfase.executeStr(this, WithdrawalRecord.class, HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.URL_WITHDRAWAL_LIST, hucParameInterfase.getRequestParameter(pageVo), false);
        loading();
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object obj) {
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                if (obj instanceof List) {
                    List<WithdrawalRecord> withdrawalRecords = (List<WithdrawalRecord>) obj;
                    if (refreshFlag) {
                        if (withdrawalRecords == null || withdrawalRecords.size() == 0) {
                            if (withdrawalRecords.size() == 0) {
                                xRecyclerView.refreshComplete(LoadingFooter.State.NoData, "");
                            } else {
                                xRecyclerView.refreshComplete(LoadingFooter.State.TheEnd, "");
                            }
                        } else {
                            for (WithdrawalRecord withdrawalRecord : withdrawalRecords) {
                                withdrawalRecordList.add(withdrawalRecord);
                            }
                            withdrawalRecordAdapter.notifyDataSetChanged();
                            xRecyclerView.refreshComplete(LoadingFooter.State.Normal, "");
                        }
                    } else {
                        withdrawalRecordList.clear();
                        for (WithdrawalRecord withdrawalRecord : withdrawalRecords) {
                            withdrawalRecordList.add(withdrawalRecord);
                        }
                        withdrawalRecordAdapter.notifyDataSetChanged();
                        xRecyclerView.refreshComplete(LoadingFooter.State.Normal, "");
                    }
                }
                break;
        }
        loadDismiss();
    }

    @Override
    protected void onStart() {
        super.onStart();
        refreshFlag = false;
        pageVo.setCurrent(1 + "");
        pageVo.setSize(10 + "");
        getInitData();
    }
}